package main

import (
	h "./handler"
	m "./middleware"
	"github.com/julienschmidt/httprouter"
	"github.com/rs/cors"
	"github.com/urfave/negroni"
	"net/http"
)

func main() {
	n := negroni.Classic()
	n.Use(cors.New(cors.Options{
		AllowedOrigins:   []string{"*"},
		AllowCredentials: true,
		AllowedHeaders:   []string{"*"},
		AllowedMethods: []string{"GET","POST","PUT","OPTIONS","DELETE"},
		Debug:            true,
	}))
	n.Use(m.CheckAuthMiddleware())
	//n.Use(m.HeaderMiddleware())

	router := httprouter.New()
	router.GET("/", h.Home)
	router.ServeFiles("/static/*filepath", http.Dir("./upload/img/"))
	router.POST("/api/v1/auth/login", h.Login)
	router.GET("/api/v1/auth/logout", h.Logout)
	router.POST("/api/v1/auth/register", h.Register)

	router.GET("/api/v1/users", h.GetUsers)
	router.POST("/api/v1/users", h.SaveUser)
	router.GET("/api/v1/users/:id", h.GetUser)
	router.PUT("/api/v1/users",h.SaveUser)
	router.DELETE("/api/v1/users/:id", h.DeleteUsers)
	//router.POST("/api/v1/users/update", h.SaveUser)
	//router.POST("/api/v1/users/delete", h.DeleteUsers)
	router.GET("/api/v1/me", h.GetCurrentUser)

	router.GET("/api/v1/groups", h.GetGroups)
	router.GET("/api/v1/groups/:id", h.GetGroup)
	router.GET("/api/v1/groups/:id/users", h.GetRelatedUsers)
	router.POST("/api/v1/groups/:id/users", h.AddRelatedUsers)
	router.POST("/api/v1/groups", h.SaveGroup)
	router.PUT("/api/v1/groups", h.SaveGroup)
	//router.POST("/api/v1/groups/delete", h.DeleteGroups)


	router.GET("/api/v1/tags", h.GetTags)
	router.GET("/api/v1/tags/:id", h.GetTag)
	router.PUT("/api/v1/tags", h.SaveTag)
	router.POST("/api/v1/tags/delete", h.DeleteTags)

	router.GET("/api/v1/itembanks", h.GetItembanks)
	router.POST("/api/v1/itembanks", h.CreateItembank)
	router.GET("/api/v1/itembanks/:id", h.GetItembank)
	router.PUT("/api/v1/itembanks/:id", h.UpdateItembank)
	router.GET("/api/v1/itembanks/:id/questions", h.GetRelatedQuestions)
	router.POST("/api/v1/itembanks/:id/questions", h.AddRelateQuestions)
	router.DELETE("/api/v1/itembanks/:id/questions", h.RemoveRelatedQuestions)
	router.POST("/api/v1/itembanks/:id/upload", h.UploadItembankImage)
	//router.POST("/api/v1/itembanks/:id/enable", h.EnableItembank)
	//router.POST("/api/v1/itembanks/:id/disable", h.DisableItembank)
	router.POST("/api/v1/itembanks/:id/status", h.ChangeStatus)
	router.POST("/api/v1/itembanks/:id/remove", h.RemoveItembank)

	router.POST("/api/v1/categories", h.CreateCategory)
	router.DELETE("/api/v1/categories", h.DeleteCategory)
	router.PUT("/api/v1/categories", h.UpdateCategory)
	router.GET("/api/v1/categories", h.GetCategories)

	router.GET("/api/v1/questions", h.GetQuestions)
	router.GET("/api/v1/questions/:id", h.GetQuestion)
	router.POST("/api/v1/questions", h.SaveQuestion)
	router.PUT("/api/v1/questions", h.SaveQuestion)
	router.DELETE("/api/v1/questions", h.DeleteQuestion)

	//router.POST("/upload", h.UploadImage)

	n.UseHandler(router)
	//defer  db.GetInstance().Close()
	n.Run(":8080")
}
